#!/bin/bash
#===============================================================================
# update.sh
#
# Update the JPF files to the blades.
#===============================================================================

#-------------------------------------------------------------------------------
# Initialize the default behaviour of the script.
#-------------------------------------------------------------------------------

DEFAULT_COPY_JPF=0
DEFAULT_COPY_EXAMPLES=0
DEFAULT_COPY_REST=0

#-------------------------------------------------------------------------------
# Load the configuration.
#-------------------------------------------------------------------------------

test -x ./config.sh && . ./config.sh &>/dev/null

# Check that the necessary configuration settings are available.

if [ "${JPF_CORE_LOCAL_DIR}" == "" ] ; then
	echo "Setting \${JPF_CORE_LOCAL_DIR} is not set"
	exit 1
fi

if [ "${JPF_SYMBC_LOCAL_DIR}" == "" ] ; then
	echo "Setting \${JPF_SYMBC_LOCAL_DIR} is not set"
	exit 1
fi

if [ "${JPF_DISTRIB_LOCAL_DIR}" == "" ] ; then
	echo "Setting \${JPF_DISTRIB_LOCAL_DIR} is not set"
	exit 1
fi

if [ "${HOST_USERNAME}" == "" ] ; then
	echo "Setting \${HOST_USERNAME} is not set"
	exit 1
fi

#-------------------------------------------------------------------------------
# Explain the command line options to the user.
#-------------------------------------------------------------------------------

function SHOW_OPTIONS() {
	EXIT_CODE=${1} ; shift

	cat <<EOF
Usage: ${0} <command>

Commands:
  all       Update all the files
  jpf       Update the JPF extensions only
  examples  Update the examples only
  help      Show this message and exit

EOF
	exit ${EXIT_CODE}
}

#-------------------------------------------------------------------------------
# Copy a single file to a remote host.
#-------------------------------------------------------------------------------

function COPY_OVER() {
	SOURCE=${1} ; shift
	DEST=${1} ; shift
	if [ -f ${SOURCE} ] ; then
		scp ${SOURCE} ${DEST}
	else
		echo "Missing file \"${SOURCE}\""
	fi
}

#-------------------------------------------------------------------------------
# Initialize the behaviour (before processing command line options).
#-------------------------------------------------------------------------------

COPY_JPF=${DEFAULT_COPY_JPF}
COPY_EXAMPLES=${DEFAULT_COPY_EXAMPLES}
COPY_REST=${DEFAULT_COPY_REST}

#-------------------------------------------------------------------------------
# Process the command line options.
#-------------------------------------------------------------------------------

COMMAND=${1} ; shift

case ${COMMAND} in
	help) SHOW_OPTIONS 0 ;;
	all) COPY_JPF=1 ; COPY_EXAMPLES=1 ; COPY_REST=1 ;;
	jpf) COPY_JPF=1 ;;
	examples) COPY_EXAMPLES=1 ;;
	*) echo "Unknown option \"${COMMAND}\"" ; SHOW_OPTIONS 1 ;;
esac

#-------------------------------------------------------------------------------
# Copy over the JPF base files if required.
#-------------------------------------------------------------------------------

if [ ${COPY_JPF} -eq 1 ] ; then
	DEST_DIR="${HOST_USERNAME}@${FIRST_HOST}:JPF"
	COPY_OVER ${JPF_CORE_LOCAL_DIR}/build/RunJPF.jar ${DEST_DIR}/RunJPF.jar
	COPY_OVER ${JPF_CORE_LOCAL_DIR}/build/jpf.jar ${DEST_DIR}/build/jpf.jar
	COPY_OVER ${JPF_CORE_LOCAL_DIR}/build/jpf-classes.jar ${DEST_DIR}/build/jpf-classes.jar
	COPY_OVER ${JPF_CORE_LOCAL_DIR}/build/jpf-annotations.jar ${DEST_DIR}/build/jpf-annotations.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/build/jpf-symbc.jar ${DEST_DIR}/build/jpf-symbc.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/build/jpf-symbc-classes.jar ${DEST_DIR}/build/jpf-symbc-classes.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/build/jpf-symbc-annotations.jar ${DEST_DIR}/build/jpf-symbc-annotations.jar
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/build/jpf-distrib.jar ${DEST_DIR}/build/jpf-distrib.jar
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/build/jpf-distrib-classes.jar ${DEST_DIR}/build/jpf-distrib-classes.jar
fi

#-------------------------------------------------------------------------------
# Copy over the supporting jar files if required.
#-------------------------------------------------------------------------------

if [ ${COPY_REST} -eq 1 ] ; then
	DEST_DIR="${HOST_USERNAME}@${FIRST_HOST}:JPF"
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/STPJNI.jar ${DEST_DIR}/lib/STPJNI.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/aima-core.jar ${DEST_DIR}/lib/aima-core.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/automaton.jar ${DEST_DIR}/lib/automaton.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/choco-1_2_04.jar ${DEST_DIR}/lib/choco-1_2_04.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/choco-solver-2.1.1-20100709.142532-2.jar ${DEST_DIR}/lib/choco-solver-2.1.1-20100709.142532-2.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/commons-lang-2.4.jar ${DEST_DIR}/lib/commons-lang-2.4.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/commons-math-1.2.jar ${DEST_DIR}/lib/commons-math-1.2.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/coral.jar ${DEST_DIR}/lib/coral.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/grappa.jar ${DEST_DIR}/lib/grappa.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/hampi.jar ${DEST_DIR}/lib/hampi.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/iasolver.jar ${DEST_DIR}/lib/iasolver.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/libcvc3.jar ${DEST_DIR}/lib/libcvc3.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/opt4j-2.4.jar ${DEST_DIR}/lib/opt4j-2.4.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/org.sat4j.core.jar ${DEST_DIR}/lib/org.sat4j.core.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/org.sat4j.pb.jar ${DEST_DIR}/lib/org.sat4j.pb.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/scale.jar ${DEST_DIR}/lib/scale.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/solver.jar ${DEST_DIR}/lib/solver.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/string.jar ${DEST_DIR}/lib/string.jar
	COPY_OVER ${JPF_SYMBC_LOCAL_DIR}/lib/yicesapijava.jar ${DEST_DIR}/lib/yicesapijava.jar
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/lib/apfloat.jar ${DEST_DIR}/lib/apfloat.jar
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/lib/commons-exec-1.2.jar ${DEST_DIR}/lib/commons-exec-1.2.jar
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/lib/green.jar ${DEST_DIR}/lib/green.jar
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/lib/jedis-2.0.0.jar ${DEST_DIR}/lib/jedis-2.0.0.jar
fi

#-------------------------------------------------------------------------------
# Copy over the example .class and .jpf files if required.
#-------------------------------------------------------------------------------

if [ ${COPY_EXAMPLES} -eq 1 ] ; then
	DEST_DIR="${HOST_USERNAME}@${FIRST_HOST}:JPF"
	scp ${JPF_DISTRIB_LOCAL_DIR}/build/examples/*.class ${DEST_DIR}/build/examples/
	scp ${JPF_DISTRIB_LOCAL_DIR}/src/examples/*.jpf ${DEST_DIR}/build/examples/
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/distrib.jpf ${DEST_DIR}
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/nondistrib.jpf ${DEST_DIR}
	COPY_OVER ${JPF_DISTRIB_LOCAL_DIR}/green.jpf ${DEST_DIR}
	COPY_OVER hostport.jpf ${DEST_DIR}
	COPY_OVER jpf.properties ${DEST_DIR}
fi

#-------------------------------------------------------------------------------
# Tell the first host to copy the new files over to the other hosts.
#-------------------------------------------------------------------------------

ssh ${HOST_USERNAME}@${FIRST_HOST} ./distribute.sh

#===============================================================================
# End of update.sh
#===============================================================================
